#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define Data_type int
typedef struct Node
{
    Data_type data;
    struct Node *next;
} node, *pnode;

typedef struct linkQueue
{
    pnode rear;
    pnode front;

} linkQueue, *plinkQueue;
// 创建
plinkQueue create_queue()
{
    plinkQueue q = (plinkQueue)malloc(sizeof(linkQueue));
    if (q == NULL)
    {
        printf("内存分配失败！\n");
    }
    // 创建头结点
    q->front = q->rear = (node *)malloc(sizeof(node));
    q->front->next = NULL;
    return q;
}
// 入队
int queue_push(plinkQueue q, Data_type value)
{
    if (q == NULL)
    {
        printf("队列为空\n");
    }
    pnode newNode = (pnode)malloc(sizeof(pnode));
    newNode->data = value;
    newNode->next = NULL;
    q->rear->next = newNode;
    q->rear = newNode;
}
// 出队
Data_type queue_pop(plinkQueue q)
{
    if (q == NULL)
    {
        printf("队列为空\n");
    }
    pnode temp = q->front->next;
    if (temp)
    {
        printf("2front---%p\n", temp);
        q->front->next = temp->next;
        Data_type value = temp->data;
        if (temp == q->rear)
        {
            q->rear = q->front;
        }
        free(temp);
        return value;
    }
    else
    {
        printf("没有数据了\n");
        return -1;
    }
}
// 打印
int queue_printf(plinkQueue q)
{
    if (q == NULL)
    {
        printf("队列为空\n");
    }
    pnode head = q->front;
    while (head->next != NULL)
    {
        head = head->next;
        printf("%d\t", head->data);
    }
    printf("\n");
}
void main()
{
    plinkQueue q = create_queue();
    queue_push(q, 1);
    queue_push(q, 2);
    queue_push(q, 3);
    queue_printf(q);
    printf("value:%d\n", queue_pop(q));
    printf("value:%d\n", queue_pop(q));
    printf("value:%d\n", queue_pop(q));
    printf("value:%d\n", queue_pop(q));
}
